﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.IO;
using Aliphant.Common.Helper;
using System.Windows.Forms;
using Aliphant.Common.Managers;

namespace Aliphant.UI.Forms.Notify
{
    public static class NotifyIconHelper
    {
        public static void Init()
        {
            notifyIcon = new NotifyIcon
            {
                //BalloonTipText = "通知内容",
                Text = "XDeskTool",
                Icon = new System.Drawing.Icon(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, "cw_launcher.ico")),
                ContextMenuStrip = GetMenu(),
            };
            AddNewNotify();
        }

        private static NotifyIcon? notifyIcon;

        /// <summary>
        /// 系统通知气泡
        /// </summary>
        /// <param name="message">消息</param>
        /// <param name="title">标题</param>
        /// <param name="timeout"></param>
        /// <param name="icon">图标（0:无 1:提示 2:警告 3:错误）</param>
        public static void ShowBalloon(string message, string title = "", int timeout = 0, NotifyLevel icon = 0)
        {
            if (notifyIcon == null) return;
            notifyIcon.BalloonTipText = message;
            //notifyIcon.ShowBalloonTip(timeout: timeout);
            notifyIcon.ShowBalloonTip(timeout, title, message, (ToolTipIcon)icon);
        }

        public static void AddNewNotify()
        {
            if (notifyIcon == null) return;

            notifyIcon.Visible = true;
            notifyIcon.MouseClick += OnNotifyClick;
            notifyIcon.MouseDoubleClick += ActiveWindow;
            //notifyIcon.ShowBalloonTip(timeout:1000);  // 弹出系统通知气泡，timeout:显示时间
        }

        /// <summary>
        /// 设置右击菜单
        /// </summary>
        /// <returns></returns>
        private static ContextMenuStrip GetMenu()
        {
            ContextMenuStrip menuStrip = new ContextMenuStrip();

            List<MenuItemDefine> list = Enum.GetValues(typeof(MenuItemDefine)).Cast<MenuItemDefine>().ToList();
            list.Remove(MenuItemDefine.None);
            foreach (var item in list) AddMenuItem(menuStrip, item);

            return menuStrip;
        }

        private static void AddMenuItem(ContextMenuStrip menuStrip, MenuItemDefine menuItem)
        {
            ToolStripMenuItem item = new ToolStripMenuItem(menuItem.GetDescription()) { Tag = menuItem };
            item.Click += OnMenuItemClick;
            menuStrip.Items.Add(item);
        }

        /// <summary>
        /// 点击菜单项事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void OnMenuItemClick(object? sender, EventArgs e)
        {
            if (sender == null) return;
            switch ((MenuItemDefine)(((ToolStripMenuItem)sender).Tag ?? MenuItemDefine.None))
            {
                case MenuItemDefine.Window:
                    ActiveWindow();
                    break;
                case MenuItemDefine.Exit:
                    ExitProgram();
                    break;
            }
        }

        #region 事件

        private static void OnNotifyClick(object? sender, MouseEventArgs e)
        {
            if (notifyIcon == null) return;

            if (e.Button == MouseButtons.Left)
            {
                notifyIcon?.ContextMenuStrip?.Hide();
                ActiveWindow();
            }
        }

        /// <summary>
        /// 激活窗口
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private static void ActiveWindow(object? sender = null, MouseEventArgs? e = null)
        {
            if (System.Windows.Application.Current.MainWindow.WindowState == WindowState.Minimized)
            {
                System.Windows.Application.Current.MainWindow.WindowState = WindowState.Normal;
            }
            System.Windows.Application.Current.MainWindow.Show();
            System.Windows.Application.Current.MainWindow.Activate();
        }

        /// <summary>
        /// 退出程序
        /// </summary>
        private static void ExitProgram()
        {
            ConfigManager.Instance.ExitApplication = true;
            System.Windows.Application.Current.MainWindow.Close();
        }

        #endregion

        /// <summary>
        /// 释放资源
        /// </summary>
        public static void Release()
        {
            if (notifyIcon == null) return;

            notifyIcon.Visible = false;
            notifyIcon.Dispose();
        }

        /// <summary>
        /// 菜单项
        /// </summary>
        private enum MenuItemDefine
        {
            None,
            [Description("主菜单")]
            Window,
            [Description("退出")]
            Exit,
        }

        public enum NotifyLevel : int
        {
            None = 0,
            Info = 1,
            Warning = 2,
            Error = 3,
        }
    }
}
